﻿define('eventsConfig',
    ['jquery', 'routeConfig', 'ko', 'specialities', 'departments', 'positions', 'providers'],
 function ($, routeConfig, ko, specialities, departments, positions, providers) {
     var
     loadFromContext = function (url, frm, m, templateName) {
         routeConfig.goToDepartmentView();
         $.getJSON(url, function (data) {
             var funcCall = "new " + templateName + "();";
             var obj = eval(funcCall);
             obj.Load(data);
             m.LoadActive(obj);
         });
     },

     configure = function () {
         $("#currentView").undelegate(".fill", "click");
         $("#currentView").delegate(".fill", "click", function () {
             var context = ko.contextFor(this);
             var obj = context.$root.entity;
             var model = context.$userViewModel;
             model.selectedObj().load(obj);
             var dlg = new MedoraDialogWindow($("#currentView"));
             dlg.save = function () {
                 model.selectedObj().save(model, dlg);
             };
             var content = $("#formContainer").html();
             dlg.createWithSave(400, 300, "Edit " + model.selectedObj().templateName, "<div id='dlg'>" + content + "</div>");

             ko.applyBindings(model.selectedObj(), $("#dlg")[0]);
             return false;
         });

         $(".mainContainer").undelegate(".btnNew", "click");
         $(".mainContainer").delegate(".btnNew", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$data;
             var dlg = new MedoraDialogWindow($("#currentView"));
             dlg.save = function () {
                model.currentObj().basicObject().selectedObj().save(model.currentObj().basicObject(), dlg);
             };
             var content = $("#formContainer").html();
             dlg.createWithSave(400, 300, "New " + model.currentObj().basicObject().selectedObj().templateName, "<div id='dlg'>" + content + "</div>");
             model.currentObj().basicObject().loadNew();
             ko.applyBindings(model.currentObj().basicObject().selectedObj(), $("#dlg")[0]);
             
             
             return false;
         });
         
         $(".mainContainer").undelegate(".btnDelete", "click");
         $(".mainContainer").delegate(".btnDelete", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$data;
             var ids = new Array();
             var deleteController = model.currentObj().basicObject().selectedObj().deleteController;
             $.each(model.currentObj().basicObject().selectedItems(), function () {
                 ids.push(this.id);
             });
             if (ids.length > 0) {
                 //todo modify for confirm window
                 MedoraConfirm($("#currentView"), "Confirm delete", "Are you sure?",
                     function (dlg) {
                         DeleteAjax(deleteController + JSON.stringify(ids), {}, function () {
                             model.currentObj().basicObject().RefreshGrid();
                             dlg.close();
                             toastr.success("Deleted");
                         
                     });
                     });
             }
             
         });

         $(".mainContainer").undelegate(".profile", "click");
         $(".mainContainer").delegate(".profile", "click", function () {
             $(".profile", ".mainContainer").removeClass("active");
             var context = ko.contextFor(this);
             var model = context.$data;
             $(this).addClass("active");

             var type = $(this).attr("object_name");
             if (type != null) {
                 ko.cleanNode(model.currentObj());
                 var funcCall = "new " + type + "();";
                 var obj = eval(funcCall);
                 model.currentObj(obj);
                 model.activeList(type+"Grid");
             }
         });

     };
     return {
         configure: configure
     };
 });